from rest_framework import serializers
from api import models
from django.forms import model_to_dict


class AuctionModelSerializer(serializers.ModelSerializer):
    status_title = serializers.CharField(source="get_status_display")
    goods = serializers.SerializerMethodField()
    preview_time = serializers.SerializerMethodField(read_only=True)
    auction_time = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = models.Auction
        fields = ["id", "title", "cover", "status", "look_count", "goods_count", "total_price",
                  "bid_count", "goods", "preview_time", "auction_time", 'deposit', 'status_title']

    def get_goods(self, obj):
        auction_item = models.AuctionItem.objects.filter(auction_id=obj.id)
        goods_item = AuctionDetailModelSerializer(instance=auction_item, many=True, context=self.context)
        return goods_item.data

    def get_preview_time(self, obj):
        preview_time = models.Auction.objects.filter(id=obj.id).first()
        return [preview_time.preview_start_time, preview_time.preview_end_time]

    def get_auction_time(self, obj):
        auction_time = models.Auction.objects.filter(id=obj.id).first()
        return [auction_time.auction_start_time, auction_time.auction_end_time]


class AuctionDetailModelSerializer(serializers.ModelSerializer):
    status_name = serializers.CharField(source="get_status_display")
    is_deposit = serializers.SerializerMethodField()

    class Meta:
        model = models.AuctionItem
        fields = ["id", "title", "status", "cover", "start_price", "unit", "is_deposit", 'reserve_price',
                  'highest_price', 'deposit', 'status_name', "deal_price"]

    def get_is_deposit(self, obj):
        user_object = self.context["request"].user
        if not user_object:
            return False
        return models.DepositRecord.objects.filter(status=2, deposit_type=1, item=obj, user=user_object).exists()


class AuctionItemModelSerializer(serializers.ModelSerializer):
    goods = serializers.SerializerMethodField()
    is_deposit = serializers.SerializerMethodField()
    status_name = serializers.CharField(source="get_status_display")

    class Meta:
        model = models.Auction
        exclude = ["video", "create_time"]

    def get_goods(self, obj):
        item_obj = models.AuctionItem.objects.filter(auction=obj, status__gt=1)
        ser = AuctionDetailModelSerializer(instance=item_obj, many=True, context=self.context)
        return ser.data

    def get_is_deposit(self, obj):
        """是否为全场保证金"""
        user_object = self.context["request"].user
        if not user_object:
            return False
        return models.DepositRecord.objects.filter(auction=obj, status=2, deposit_type=2, user=user_object,
                                                   item__isnull=True).exists()


class AuctionItemDetailViewModelSerializer(serializers.ModelSerializer):
    image_list = serializers.SerializerMethodField()
    carousel_list = serializers.SerializerMethodField()
    detail_list = serializers.SerializerMethodField()
    record = serializers.SerializerMethodField()

    class Meta:
        model = models.AuctionItem
        fields = "__all__"

    def get_image_list(self, obj):
        queryset = models.AuctionItemImage.objects.filter(item=obj).order_by("-order")
        return [row.img for row in queryset]

    def get_carousel_list(self, obj):
        queryset = models.AuctionItemImage.objects.filter(item=obj, carousel=True).order_by("-order")
        return [row.img for row in queryset]

    def get_detail_list(self, obj):
        queryset = models.AuctionItemDetail.objects.filter(item=obj)
        return [model_to_dict(row, ['key', 'value']) for row in queryset]

    def get_record(self, obj):
        queryset = models.BrowseRecord.objects.filter(item=obj)
        result = {
            'record_list': [row.user.avatar for row in queryset[0:10]],
            'total_count': queryset.count()
        }
        return result


class AuctionDepositViewModelSerializer(serializers.ModelSerializer):
    deposit = serializers.SerializerMethodField()
    balance = serializers.SerializerMethodField()
    auction_id = serializers.SerializerMethodField()

    class Meta:
        model = models.AuctionItem
        fields = ["id", "title", "cover", "reserve_price", "highest_price", "deposit", "balance", "start_price",
                  "auction_id"]

    def get_balance(self, obj):
        return self.context['request'].user.balance

    def get_deposit(self, obj):
        result = {
            'seleted': 1,
            'data_list': [
                {"id": 1, 'price': obj.deposit, 'text': "单场保证金"},
                {"id": 2, 'price': obj.auction.deposit, 'text': "全场保证金"},
            ]
        }
        return result

    def get_auction_id(self, obj):
        return obj.auction.id


class AuctionItemImgaeModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.AuctionItemImage
        fields = "__all__"


class TopicViewModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Topic
        fields = "__all__"
